<template>
  <div class="login-container">
    <!-- 左侧背景区域 -->
    <div class="left-section">
      <div class="system-info">
        <h1>MES 生产管理系统</h1>
        <p>Manufacturing Execution System</p>
      </div>
    </div>

    <!-- 右侧登录区域 -->
    <div class="right-section">
      <div class="form-container">
        <div class="form-header">
          <h2>{{ isLogin ? '用户登录' : '用户注册' }}</h2>
          <p class="switch-text">
            {{ isLogin ? '还没有账号?' : '已有账号?' }}
            <span @click="switchMode">{{ isLogin ? '立即注册' : '去登录' }}</span>
          </p>
        </div>
        <!-- 登录表单 -->
        <div v-if="isLogin" class="form">
          <div class="form-item">
            <el-icon>
              <User />
            </el-icon>
            <input v-model="loginForm.username" type="text" style="width: 80%;" placeholder="请输入用户名">
          </div>
          <div class="form-item">
            <el-icon>
              <Lock />
            </el-icon>
            <input v-model="loginForm.password" type="password" style="width: 80%;" placeholder="请输入密码">
          </div>
          <div class="remember-forgot">
            <label>
              <input type="checkbox" v-model="loginForm.remember">
              记住密码
            </label>
            <span class="forgot-password">忘记密码?</span>
          </div>
          <button class="submit-btn" @click="handleLogin">登录</button>
        </div>
        <!-- 注册表单 -->
        <div v-else class="form">
          <div class="form-item">
            <el-icon>
              <User />
            </el-icon>
            <input v-model="registerForm.username" type="text" style="width: 80%;" placeholder="请输入用户名">
          </div>
          <div class="form-item">
            <el-icon>
              <Message />
            </el-icon>
            <input v-model="registerForm.email" type="email" style="width: 80%;"  placeholder="请输入邮箱">
          </div>
          <div class="form-item">
            <el-icon>
              <Lock />
            </el-icon>
            <input v-model="registerForm.password" type="password" style="width: 80%;" placeholder="请输入密码">
          </div>
          <div class="form-item">
            <el-icon>
              <Lock />
            </el-icon>
            <input v-model="registerForm.confirmPassword" type="password" style="width: 80%;" placeholder="请确认密码">
          </div>
          <button class="submit-btn" @click="handleRegister">注册</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'UserLogin',
  data() {
    return {
      isLogin: true,
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  methods: {
    switchMode() {
      this.isLogin = !this.isLogin
    },
    async handleLogin() {
      try {
        const response = await this.$api.login({
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        if (response.data.code == 200) {
          this.$message.success("登录成功");
          this.loginForm.username = '';
          this.loginForm.password = '';
          this.$router.push('/AppMenu');

        } else {
          this.$message.error(response.data.message);
        }
      } catch (error) {
        console.error('登录错误：', error);
      }
    },
    handleRegister() {
      // 实现注册逻辑
      console.log('注册表单:', this.registerForm)
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

.left-section {
  flex: 1;
  background: linear-gradient(135deg, #1976d2, #64b5f6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.system-info {
  text-align: center;
}

.system-info h1 {
  font-size: 2.5em;
  margin-bottom: 10px;
}

.right-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.form-container {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  color: #333;
  margin-bottom: 10px;
}

.switch-text {
  color: #666;
  font-size: 14px;
}

.switch-text span {
  color: #1976d2;
  cursor: pointer;
}

.form-item {
  position: relative;
  margin-bottom: 20px;
}

.form-item input {
  width: 100%;
  padding: 12px 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-item input:focus {
  border-color: #1976d2;
  outline: none;
  box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.1);
}

.form-item i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 14px;
  color: #666;
}

.forgot-password {
  color: #1976d2;
  cursor: pointer;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #1976d2;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.submit-btn:hover {
  background: #1565c0;
}
</style>
